package handler

import (
	"github.com/name5566/leaf/gate"
	"github.com/name5566/leaf/log"
	"server/game/player"
	"server/game/translate"
	"server/msg/jsonData"
	"time"
)

func CreatePlayerHandler(args []interface{}) {
	msg := args[0].(*jsonData.MsgCreatePlayerReq)
	agent := args[1].(gate.Agent)
	userInfo, err := player.CreatePlayer(msg)
	if err != nil {
		log.Error("Failed to create player to database: %v", err)
		agent.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: "请不要重复创建", RequestId: msg.RequestId})
		return
	}
	response := jsonData.MsgRes{
		Ret:       jsonData.OK,
		Result:    userInfo,
		RequestId: msg.RequestId,
	}
	agent.WriteMsg(&response)
}
func GetPlayerInfoHandler(args []interface{}) {
	msg := args[0].(*jsonData.MsgGetPlayerInfoReq)
	agent := args[1].(gate.Agent)
	userInfo, err := player.GetPlayerInfoHandler(msg.PlatformId)
	if err != nil {
		log.Error("Failed to create player to database: %v", err)
		agent.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: "请不要重复创建", RequestId: msg.RequestId})
		return
	}
	response := jsonData.MsgRes{
		Ret:       jsonData.OK,
		Result:    userInfo,
		RequestId: msg.RequestId,
	}
	agent.WriteMsg(&response)
}
func UnLock(args []interface{}) {
	msg := args[0].(*jsonData.MsgUnLockSystem)
	agent := args[1].(gate.Agent)
	lock, err := player.UnLock(msg.UserId, msg.SId)
	if err != nil {
		log.Error("Failed to create player to database: %v", err)
		agent.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: msg.RequestId})
		return
	}
	agent.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: lock, RequestId: msg.RequestId})
}
func GetPlayerInfoByUidHandler(args []interface{}) {
	msg := args[0].(*jsonData.MsgSystemList)
	agent := args[1].(gate.Agent)
	userInfo, err := player.GetPlayerInfoByUid(msg.UserId)
	if err != nil {
		log.Error("Failed to create player to database: %v", err)
		agent.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: msg.RequestId})
		return
	}
	agent.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: userInfo, RequestId: msg.RequestId})
}
func UpPlayerLevel(args []interface{}) {
	msg := args[0].(*jsonData.MsgUpPlayerLevelReq)
	agent := args[1].(gate.Agent)
	ret, err := player.UpPlayerLevel(msg.UserId)
	if err != nil {
		agent.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: msg.RequestId})
		return
	}
	agent.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: ret, RequestId: msg.RequestId})
}
func ShouYi(args []interface{}) {
	msg := args[0].(*jsonData.MsgShouYiReq)
	agent := args[1].(gate.Agent)
	playerInfo, err := player.EnGetUserInfo(msg.UserId)
	timeSecond := time.Now().Unix()
	diffTime := timeSecond - playerInfo.LastShouRuTime
	ret, err := translate.GetTranslateShouYi(msg.UserId)
	player.EnUpdateLiXianShouYi(msg.UserId, timeSecond)
	if diffTime >= 16200 {
		diffTime = 16200
	}
	ret["diff_time"] = diffTime
	if err != nil {
		agent.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: msg.RequestId})
		return
	}
	agent.WriteMsg(&jsonData.MsgShouYiRes{Ret: 0, Result: ret, RequestId: msg.RequestId})
}
